﻿using Candy.Model;
using Candy.Web.Data;
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Candy.ViewModel
{
    public class JobGroupViewModel : ViewModelBase
    {
        private ObservableCollection<JobViewModel> _jobs;
        public ObservableCollection<JobViewModel> Jobs
        {
            get
            {
                return _jobs;
            }
            set
            {
                _jobs = value;
                RaisePropertyChanged("Jobs");
            }
        }

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }

        internal Mode GroupMode { get; private set; }

        public int GroupId { get; private set; }

        IDataService _dataService;
        public JobGroupViewModel(IDataService dataService, IEnumerable<BlastJobDto> jobs)
        {
            _dataService = dataService;
            _jobs = new ObservableCollection<JobViewModel>();
            if (jobs.Count() > 0)
            {

                if (jobs.First().Task == BlastTask.blast_fishing_dbs)
                {
                    if (jobs.Select(x => x.FamilyId.Value.ToString()).Distinct().Count() > 1)
                        throw new ArgumentException("All jobs must have the same Family or Bait Uid.");
                    GroupMode = Mode.Family;
                    GroupId = jobs.First().FamilyId.Value;
                    if (!IsInDesignMode)
                        Name = jobs.First().FamilyName; //.Instance.Families.Single(x => x.Id == GroupId).Name;
                    else
                        Name = "MyFamily";
                }
                else if (jobs.First().Task == BlastTask.find_ortholog_genes)
                {
                    if (jobs.Select(x => x.BaitId.Value.ToString()).Distinct().Count() > 1)
                        throw new ArgumentException("All jobs must have the same Family or Bait Uid.");
                    GroupMode = Mode.Bait;
                    GroupId = jobs.First().BaitId.Value;
                    if (!IsInDesignMode)
                        Name = jobs.First().BaitName; //DataAccess.Instance.Genes.Single(x => x.Id == GroupId).Name;
                    else
                        Name = "MyBait";
                }

                _jobs = new ObservableCollection<JobViewModel>(jobs.Select(x => new JobViewModel(dataService, x)));
            }
        }

        public JobGroupViewModel(IDataService dataService,string name, int  groupId, Mode mode)
        {
            _dataService = dataService;
            _jobs = new ObservableCollection<JobViewModel>();
            GroupId = groupId;
            GroupMode = mode;
            Name = name;
        }

        public void Merge(IEnumerable<BlastJobDto> jobs)
        {
            if (GroupMode == Mode.Family)
            {
                var matchs = jobs.Where(x => x.FamilyId == GroupId).Except(_jobs.Select(x => x.Entity));
                foreach (var item in matchs)
                {
                    _jobs.Add(new JobViewModel(_dataService, item));
                }

            }
            else
            {
                var matchs = jobs.Where(x => x.BaitId == GroupId).Except(_jobs.Select(x => x.Entity));
                foreach (var item in matchs)
                {
                    _jobs.Add(new JobViewModel(_dataService, item));
                }

            }

        }

        public enum Mode
        {
            Family,
            Bait
        }
    }
}
